<!--  自带加载按钮的列表-->
<template>
  <div class="listBox">
    <div v-if="list && list.length > 0" class="list">
      <slot></slot>
    </div>
    <!-- 无数据 -->
    <div class="btn-box" v-if="list && list.length == 0 && !loading">
      <common-no-data />
    </div>
    <!-- 加载状态 -->
    <div class="btn-box" v-if="list.length !== 0">
      <span class="btn-txt default" v-if="finished">
        {{ status }}
      </span>
      <span class="btn-txt hand" @click="getListFun" v-else>
        {{ status }}
      </span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    getListApi: {
      type: Function,
    },
  },
  data() {
    return {
      list: [],
      form: {
        pageSize: 10,
        pageNum: 1,
      },
      total: 0,
      loading: true,
      finished: false,
      status: this.$t("no-more"),
    };
  },
  computed: {},
  mounted() {
    this.getListFun();
  },
  methods: {
    getListFun() {
      this.invoke(this.getListApi, this.form).then((res) => {
        if (res && res.code == 200) {
          if (res.rows && res.rows.length > 0) {
            this.list = this.list.concat(res.rows);
          }
          this.total = res.total;
          this.form.pageNum++;
          // 加载状态结束
          this.loading = false;
          // 数据全部加载完成
          if (this.total <= this.list.length) {
            this.finished = true;
            this.status = this.$t("no-more");
          } else {
            this.status = this.$t("loading-more");
          }
          this.$emit("setList", this.list);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.listBox {
  .btn-box {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 30px;
    width: 100%;
    .btn-txt {
      text-align: center;
      &.default {
      }
      &.hand {
        cursor: pointer;
      }
    }
  }
}
</style>
